# -*- coding: UTF-8 -*-

import base
import re

class Ipicture(base.Core):

    __label__ = "IPicture"
    __url__ = "www.ipicture.ru"
    
    def uploadFile(self, image):
        params = {"userfile": open(image, "rb"), "optimize":'yes', "status": "status",
                "quality": "85", "thumb_resize_on":'on', "thumb_resize":"180",
                "method": "file"}
        f = self.opener.open("http://ipicture.ru/Upload/", params)
        info = f.info()
        newurl = info.getheader("Refresh").split(";url=")[1]
        f.close()
        f = self.opener.open(newurl)
        data = f.read()
        f.close()
        return data
        
    def response(self, data):
        data = data.decode("cp1251")
        self.image_link = re.search("\[IMG\](.+?)\[/IMG\]", data).group(1)
        r = re.search(":</b></font>\n.*?\[URL=(.+?)\]\[IMG\](.+?)\[/IMG\]\[/URL\]", data)
        self.ad_link = r.group(1)
        self.thumb_link = r.group(2)
        self.image_name = re.search(".*/(.+)", self.image_link).group(1)
